﻿package net.wangit.view.element.util;

import net.wangit.Element;
import net.wangit.Row;
import net.wangit.Unit;
import net.wangit.context.ActionContext;
import net.wangit.framework.expression.ConstantExpression;
import net.wangit.framework.expression.DataExpression;
import net.wangit.framework.expression.LabelExpression;
import net.wangit.framework.expression.OrgExpression;
import net.wangit.framework.expression.Request2SessionExpression;
import net.wangit.framework.expression.RequestExpression;
import net.wangit.framework.expression.SessionExpression;
import net.wangit.framework.expression.UserExpression;
import net.wangit.framework.expression.VariableExpression;
import net.wangit.util.Checker;
import net.wangit.view.element.ElementComponent;
import net.wangit.view.unit.UnitComponent;
import net.wangit.view.util.Html;

public class ElementUtil {

	private ElementUtil() {
	}

	public static String resolveTitle(ActionContext ac, ElementComponent ec,
			Row row) {
		String title = LabelExpression.resolve(ac, ec.getTitle());
		title = DataExpression.resolve(title, row);
		return title;
	}

	public static String resolveLink(ActionContext ac, ElementComponent ec,
			Row row) {
		String link = LabelExpression.resolve(ac, ec.getLink());
		link = DataExpression.resolve(link, row);
		return link;
	}

	public static String resolveCustom(ActionContext ac, ElementComponent ec,
			Row row) {
		return resolve(ac, row, ec.getCustom());
	}

	public static String resolveName(ActionContext ac, Element element) {
		return LabelExpression.resolve(ac, element.getName());
	}

	public static String getAlias(ElementComponent ec) {
		Unit unit = ec.getParent();
		if (unit == null)
			return ec.getId();
		else
			return getAlias(unit.getId(), ec.getId());
	}

	public static String getAlias(String unitId, String elementId) {
		return (new StringBuilder(String.valueOf(unitId))).append(".").append(
				elementId).toString();
	}

	public static String getValueOrDefault(ActionContext ac,
			ElementComponent ec, Row row) {
		String value = row != null ? row.get(ec.getId()) : null;
		return value != null ? value : resolve(ac, row, ec.getDefaultValue());
	}

	public static String getValueOrDefaultWithFormat(ActionContext ac,
			ElementComponent ec, Row row) {
		return formatDateOrTime(ec, getValueOrDefault(ac, ec, row));
	}

	private static String resolve(ActionContext ac, Row row, String expression) {
		if (!Checker.isEmpty(expression) && expression.indexOf("$") >= 0) {
			expression = ConstantExpression.resolve(expression);
			expression = VariableExpression.resolve(expression);
			expression = UserExpression.resolve(ac, expression);
			expression = OrgExpression.resolve(ac, expression);
			expression = RequestExpression.resolve(ac, expression);
			expression = SessionExpression.resolve(ac, expression);
			expression = Request2SessionExpression.resolve(ac, expression);
			expression = DataExpression.resolve(expression, row);
		}
		return expression;
	}

	public static String getUrl(ElementComponent ec) {
		UnitComponent uc = ec.getParent();
		if (uc == null)
			return ec.getId();
		else
			return (new StringBuilder("/")).append(uc.getDomain()).append("/")
					.append(uc.getId()).append("/").append(ec.getId())
					.toString();
	}

	public static StringBuffer getActionUrl(ElementComponent ec) {
		UnitComponent uc = ec.getParent();
		return getActionUrl(uc.getDomain(), uc.getId(), ec.getId());
	}

	private static StringBuffer getActionUrl(String domain, String unitId,
			String elementId) {
		StringBuffer url = new StringBuffer("/");
		url.append(domain).append("/").append(unitId).append("/").append(
				elementId).append(".do");
		return url;
	}

	public static void setHidden(ActionContext ac, StringBuffer html,
			ElementComponent ec, Row row) {
		html.append("<input type=\"hidden\" name=\"").append(getAlias(ec))
				.append("\"");
		Html.setValue(html, getValueOrDefault(ac, ec, row));
		html.append("/>");
	}

	public static void setHiddenWithFormat(ActionContext ac, StringBuffer html,
			ElementComponent ec, Row row) {
		html.append("<input type=\"hidden\" name=\"").append(getAlias(ec))
				.append("\"");
		Html.setAttribute(html, resolveCustom(ac, ec, row));
		Html.setValue(html, getValueOrDefaultWithFormat(ac, ec, row));
		html.append("/>");
	}

	private static String formatDateOrTime(ElementComponent ec, String value) {
		if (Checker.isEmpty(value) || value.length() < 19)
			return value;
		String validator = ec.getValidator();
		if (Checker.isEmpty(validator))
			return value;
		if ("3".equals(validator)) {
			String temp = value.substring(0, 10);
			if (Checker.isDate(temp))
				return temp;
		} else if ("4".equals(validator)) {
			String temp = value.substring(11);
			int index = temp.indexOf(".");
			if (index > 0)
				temp = temp.substring(0, index);
			if (Checker.isTime(temp))
				return temp;
		} else if ("0".equals(validator)) {
			int index = value.indexOf(".");
			if (index > 0) {
				String temp = value.substring(0, index);
				if (Checker.isTimestamp(temp))
					return temp;
			}
		}
		return value;
	}
}
